package com.yangcangclub.commons.annotation;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yangcangclub.commons.config.SysConfig;
import com.yangcangclub.commons.result.ResultData;
import com.yangcangclub.commons.utils.HttpUtils;
import com.yangcangclub.commons.utils.JsonUtils;
import com.yangcangclub.commons.utils.StringUtil;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

@Component
public class UserAuth {

    @Autowired
    private SysConfig sysConfig;

    @Autowired
    RedisTemplate redisTemplate;

    public Map<String,Object> getUserFullInfoByAccessToken(String access_token){

        Map<String,Object> map = Maps.newHashMap();

        if(StringUtil.isEmpty(access_token)) {
            return null; //invalid access_token
        }

        //FIXME traveler api load user data from cahce ,so send request per eggcard request,no cahce here!
        Map<String,String> params= Maps.newHashMap();
        params.put("access_token",access_token);
        String result= HttpUtils.doGet(sysConfig.getAjaxUrlHead() + sysConfig.getUserAccessTokenUrl(), params);
        ResultData resultData =(ResultData) JsonUtils.parseObject(result, ResultData.class);
        int responseCode = resultData.getCode();
        if(1000!=responseCode) {
            //TODO return user access_token invaild
            return null;
        } else {
            Map<String,Object> m= (Map)resultData.getData();
            if(m.size()>0&&null!=m.get("userInfo")&&null!=m.get("privilege")) {
//                    User remoteUserInfo = (User)
//                    Set<String> remotePrivilege= (Set<String>)m.get("privilege");

                JSONObject jsonObject = (JSONObject)m.get("userInfo");
                JSONArray jsonArray = (JSONArray)m.get("privilege");
                User user = new User();
                user.setAccessToken((String) jsonObject.get("accessToken"));
                user.setBackground((String) jsonObject.get("background"));
                user.setBrief((String) jsonObject.get("brief"));
                user.setEmail((String) jsonObject.get("email"));
                user.setHometown((String) jsonObject.get("hometown"));
                user.setId((int) jsonObject.get("id"));
                user.setIs_married((int) jsonObject.get("is_married"));
                user.setJob((String) jsonObject.get("job"));
                user.setLocation((String) jsonObject.get("location"));
                user.setMiddle_school((String) jsonObject.get("middle_school"));
                user.setName((String) jsonObject.get("name"));
                user.setNickName((String) jsonObject.get("nickName"));
                user.setPhone((String) jsonObject.get("phone"));
                user.setPhoto((String) jsonObject.get("photo"));
                user.setReal_name((String) jsonObject.get("real_name"));
                user.setSchools((String) jsonObject.get("schools"));
                user.setSex((int) jsonObject.get("sex"));
                user.setUuid((String) jsonObject.get("uuid"));
                map.put("userInfo",user);
                Set<String> set = Sets.newHashSet();
                if(jsonArray.size()>0) {
                    for (int i=0;i<jsonArray.size();i++) {
                        set.add((String)jsonArray.get(i));
                    }
                }
                map.put("privilege",set);
            } else {
                //TODO return user access_token invaild
                return null;
            }
        }

        //本机cache获取access_token对应的用户id
//        Object obj= redisTemplate.boundValueOps("user.access_token." + access_token+".id").get();
//        if(obj==null) {
//
//
//
//            int userId=(Integer)obj;
//            BoundValueOperations bo=redisTemplate.boundValueOps("user.id."+userId+".info");
//            User user= JSON.parseObject(bo.get().toString(), User.class);
//            user.setAccessToken(access_token);
//
//            BoundSetOperations privisBo=redisTemplate.boundSetOps("user.access_token." + access_token + ".privilege");
//            Set<String> set= privisBo.members();
//            map.put("userInfo",user);
//            map.put("privilege",set);
//
//            return map;
//        }
        return map;
    }
}
